import Vue from 'vue'
import Router from 'vue-router'
import Cookies from 'js-cookie'
import { Message } from 'element-ui'
Vue.use(Router)
const routes = [
    {
        path: '',
        name: 'Index',
        component: () => import("@/views/index")
    },
    {
        path: '/views',
        name: 'Index',
        component: () => import("@/views")
    },
    {
        path: '/search',
        name: 'IndexSearch',
        component: () => import("@/views/search")
    },
    {
        path: '/member',
        name: 'memberCenter',
        meta:{isAuth:true,title:''},
        component: () => import("@/views/member")
    },
    {
        path: '/member/passengers',
        name: 'memberPassengers',
        meta:{isAuth:true,title:''},
        component: () => import("@/views/member/passengers")
    },
    {
        path: '/member/address',
        name: 'memberAddress',
        meta:{isAuth:true},
        component: () => import("@/views/member/address")
    },
    {
        path: '/order',
        name: 'order',
        meta:{isAuth:true,title: '订单管理'},
        component: () => import("@/views/order")
    },
    {
        path: '/order/orderProcessing',
        name: 'orderProcessing',
        meta:{isAuth:true,title: '订单管理'},
        component: () => import("@/views/order/orderProcessing")
    },
    {
        path:'/reservation',
        name:'reservation',
        meta:{isAuth:true,title:'航班预订',},
        component: () => import("@/views/reservation")
    },
    {
        path:'/reservation-success',
        name:'ReservationSuccess',
        meta:{isAuth:true,title:'航班预订成功',},
        component: () => import("@/views/reservation/success")
    },
    {
        path:'/reservation-fail',
        name:'ReservationFail',
        meta:{isAuth:true,title:'航班预订失败',},
        component: () => import("@/views/reservation/fail")
    },
    {
        path:'/register',
        name:'register',
        meta:{title:'注册'},
        component: () => import("@/views/register")
    },
    {
        path:'/login',
        name:'login',
        meta:{title:'登录'},
        component: () => import("@/views/components/Login")
    }
]

const router = new Router({
    mode: 'hash',
    scrollBehavior: () => ({ y: 0 }),
    routes: routes
})

// 全局前置路由守卫--初始化的时候被调用、每次路由切换之前被调用
router.beforeEach((to, from, next) => {
    if(to.meta.isAuth) {    //判断当前路由是否需要进行权限控制
        // 判断是否登陆token
        if(Cookies.get('Flight-Token')){    //权限控制的具体规则
            next()
        }else {
            Message.error({
                message:"还没有登录哦，请先登录 ⊙﹏⊙",
                center: true,
            });
        }
    } else {
        next() // 放行
    }
})
// 全局后置路由守卫--初始化的时候被调用、每次路由切换之后被调用
router.afterEach((to,from)=>{
    document.title =to.meta.title || '迅盾互联航空'
})
export default router


// export default new Router({
//     mode: 'hash',
//     scrollBehavior: () => ({ y: 0 }),
//     routes: routes
// })
